/*
 * @Author: Dorange.Wang
 * @Date: 2021-04-15 14:25:15
 * @LastEditors: wangming
 * @LastEditTime: 2022-03-02 16:57:06
 * @Description: file content
 */
// 当前用户信息
import { Accesstoken } from '@/utils/auth'
import { MOS_ENABLED } from '@/utils/const'

// import { closeWindow } from '@/utils/util'


import { getHmuserUserbytoken } from '@/apis/paas/HmUserController.js'
import { getHmpersonaluserDetail } from '@/apis/user-centre/HmPersonalUserController.js'
import { getHmprocessruntimeTaskTodoCount } from '@/apis/paas/HmProcessRuntimeController.js'
import router from '@/router'
import { Message } from 'element-ui'
import stores from 'store2'
import store from '@/store'
const state = {
  userinfo: {},
  msgNum: '',
  processNum: '',
  inWelcome: false,//进入欢迎模式 未设置权限的情况下
}
const mutations = {
  SET_USER_INFO(state, userinfo) {
    state.userinfo = userinfo
  },
  SET_MSG_NUM(state, num) {
    state.msgNum = num
  },
  SET_PROCESS_NUM(state, num) {
    state.processNum = num
  },
  SET_WELCOME(state, inWelcome) {
    state.inWelcome = inWelcome
  }
}
const actions = {
  async goWelcomePage({ commit }) {
    commit('SET_WELCOME', true)
    await router.push('/app/welcome')
  },
  async logout({ commit }) {
    Accesstoken.setAccessToken('')
    stores.set(MOS_ENABLED, '')
    if (store.getters.MOS_ENABLED && store.getters.MOS_DOMAIN) {
      setTimeout(() => {
        window.location.replace(store.getters.MOS_DOMAIN)
      }, 500)
    } else if (process.env.VUE_APP_USER === 'APP') {
      await router.push('/')
      commit('CLEAR_VISITED_VIEW')
      commit('CLEAR_ROUTES')
      commit('CLEAR_BLACK_BUTTONS')
      commit('CLEAR_BLACK_BUTTONS_PROMISE')
      commit('SET_USER_INFO', {})
    } else if (process.env.VUE_APP_USER === 'ADMIN') {
      if (process.env.NODE_ENV === 'production') {
        setTimeout(() => {
          window.location.replace(window.location.origin)
        }, 500)
      } else {
        Message.warning('logout')
      }
    }
  },

  // 获取应用用户的用户信息
  async loadUserInfo({ commit, state: { userinfo } }) {
    if (userinfo.id) return
    const info = await getHmuserUserbytoken()
    commit('SET_USER_INFO', info)
    // 获取流程中心提醒
    const msgData = await getHmprocessruntimeTaskTodoCount()
    commit('SET_PROCESS_NUM', msgData)
  },

  // 获取租户用户的用户信息
  async loadTenantUserInfo({ commit }) {
    const data = await getHmpersonaluserDetail()
    commit('SET_USER_INFO', data)
  }
}
const getters = {
  getforWelcom({ inWelcome }) {
    return inWelcome
  },
  getUserVal({ userinfo }) {
    return userinfo
  },
  getMsgNum({ msgNum }) {
    return msgNum
  },
  getProcessNum({ processNum }) {
    return processNum
  }
}

export default {
  getters,
  state,
  mutations,
  actions
}
